#include <iostream>
#include <fstream>
#include "contacts.pb.h"
using namespace std;
const char *filename = "./contacts.bin";

void printContacts(contacts::contacts contacts)
{
    for (int i = 0; i < contacts.constact_size(); ++i)
    {
        const contacts::PeopleInfo &people = contacts.constact(i);
        cout << "------------联系⼈" << i + 1 << "------------" << endl;
        cout << "姓名：" << people.name() << endl;
        cout << "年龄：" << people.age() << endl;
        int j = 1;
        for (const contacts::PeopleInfo_Phone &phone : people.phone())
        {
            cout << "电话" << j++ << ": " << phone.phone_num();
            cout << " (" << phone.PhoneType_Name(phone.type()) << ")" << endl;
        }

        if (people.has_data() && people.data().Is<contacts::Address>())
        {
            contacts::Address address;
            people.data().UnpackTo(&address);
            if (!address.home_address().empty())
            {
                cout << "家庭地址：" << address.home_address() << endl;
            }
            if (!address.unit_address().empty())
            {
                cout << "单位地址：" << address.unit_address() << endl;
            }
        }

        switch (people.other_contact_case())
        {
        case contacts::PeopleInfo::OtherContactCase::kQq:
            cout << "qq号: " << people.qq() << endl;
            break;
        case contacts::PeopleInfo::OtherContactCase::kWeixin:
            cout << "微信号: " << people.weixin() << endl;
            break;
        case contacts::PeopleInfo::OtherContactCase::OTHER_CONTACT_NOT_SET:
            break;
        }

        if (people.remark_size())
        {
            cout << "备注信息: " << endl;
        }
        for (auto it = people.remark().cbegin(); it != people.remark().cend();++it)
        {
            cout << " " << it->first << ": " << it->second << endl;
        }
    }
}

int main()
{
    contacts::contacts contacts;
    fstream input(filename, ios::in | ios::binary);
    if (!input)
    {
        cerr << "文件打开失败！" << endl;
        input.close();
        return -1;
    }
    else if (!contacts.ParseFromIstream(&input))
    {
        cerr << "文件反序列化失败！" << endl;
        input.close();
        return -1;
    }
    else
    {
    }

    // 打印联系人
    printContacts(contacts);
    input.close();
    return 0;
}